﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using IceCS.GoodsSale.BLL;
using IceCS.GoodsSale.Models.UserModels;

namespace IceCS.GoodsSale.Winform
{
    public partial class FrmAddUser : Form
    {
        public FrmAddUser()
        {
            InitializeComponent();
        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            try
            {
                string userName = txtUserName.Text.Trim();
                string userPassword1 = txtUserPassword1.Text.Trim();
                string userPassword2 = txtUserPassword2.Text.Trim();
                UserManager userManager = new UserManager();
                if (userManager.UserIsExist(userName))
                {
                    MessageBox.Show("你注册的用户名已存在", "提示");
                    txtUserName.Text = "";
                    txtUserPassword1.Text = "";
                    txtUserPassword2.Text = "";
                    txtUserName.Focus();
                    return;
                }
                string regexUser = @"^[a-zA-Z]\w{4,31}$";
                if (!Regex.IsMatch(userName, regexUser))
                {
                    MessageBox.Show("用户名格式不正确，请重新输入", "提示");
                    txtUserName.Text = "";
                    txtUserPassword1.Text = "";
                    txtUserPassword2.Text = "";
                    txtUserName.Focus();
                    return;
                }
                if (userPassword1.Length < 6 || userPassword1.Length > 32)
                {
                    MessageBox.Show("密码必须为6-32个字符", "提示");
                    txtUserPassword1.Text = "";
                    txtUserPassword2.Text = "";
                    txtUserPassword1.Focus();
                    return;
                }
                if (userPassword1 != userPassword2)
                {
                    MessageBox.Show("两次输入的密码不一致", "提示");
                    txtUserPassword1.Text = "";
                    txtUserPassword2.Text = "";
                    txtUserPassword1.Focus();
                    return;
                }
                User user = new User();
                user.UserName = userName;
                user.UserPassword = userPassword1;
                UserPurview userPurview = new UserPurview();
                userPurview.UserName = userName;
                PublicMethodsManager publicMethodsManager = new PublicMethodsManager();
                if (publicMethodsManager.AddUserToTwoTable(user, userPurview))
                {
                    MessageBox.Show("用户注册成功", "提示");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("用户注册失败，请重试", "提示");
                    txtUserName.Text = "";
                    txtUserPassword1.Text = "";
                    txtUserPassword2.Text = "";
                    txtUserName.Focus();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtUserPassword2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnAddUser_Click(sender, e);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
